﻿using DataDefine;
using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UsingEventAggregator.Core;

namespace TLDNs.ViewModels
{

    public class GeneralSettingPar : BindableBase
    {
        private int _UserAccessLv;
        public int UserAccessLv
        {
            get { return _UserAccessLv; }
            set
            {
                SetProperty(ref _UserAccessLv, value);
                _ea.GetEvent<GeneralSettingIsChangeEvent>().Publish(this);
            }
        }

        private string _REC_MSG_HzChangeMsg = "";
        public string REC_MSG_HzChangeMsg
        {
            get { return _REC_MSG_HzChangeMsg; }
            set
            {
                SetProperty(ref _REC_MSG_HzChangeMsg, value);
            }
        }

        private int _REC_MSG_Hz;
        public int REC_MSG_Hz
        {
            get { return _REC_MSG_Hz; }
            set
            {
                if (value != _REC_MSG_Hz)
                {
                    REC_MSG_HzChangeMsg = GeneralSettingWindowViewModel.Getmsg();
                }
                SetProperty(ref _REC_MSG_Hz, value);
            }
        }




        private string _SEND_MSG_HzChangeMsg = "";
        public string SEND_MSG_HzChangeMsg
        {
            get { return _SEND_MSG_HzChangeMsg; }
            set
            {
                SetProperty(ref _SEND_MSG_HzChangeMsg, value);
            }
        }

        private int _SEND_MSG_Hz;
        public int SEND_MSG_Hz
        {
            get { return _SEND_MSG_Hz; }
            set
            {
                if (value != _SEND_MSG_Hz)
                {
                    SEND_MSG_HzChangeMsg = GeneralSettingWindowViewModel.Getmsg();
                }
                SetProperty(ref _SEND_MSG_Hz, value);

            }
        }

        private bool _ExportReadOnly;
        public bool ExportReadOnly
        {
            get { return _ExportReadOnly; }
            set { SetProperty(ref _ExportReadOnly, value); }
        }

        private bool _EqualWrite;
        public bool EqualWrite
        {
            get { return _EqualWrite; }
            set { SetProperty(ref _EqualWrite, value); }
        }


        private bool _AutoSaveToInherSFM;
        public bool AutoSaveToInherSFM
        {
            get { return _AutoSaveToInherSFM; }
            set { SetProperty(ref _AutoSaveToInherSFM, value); }
        }

        private int _CanSendModel;
        public int CanSendModel
        {
            get { return _CanSendModel; }
            set { SetProperty(ref _CanSendModel, value); }
        }

        private int _CanComSendCount;
        public int CanComSendCount
        {
            get { return _CanComSendCount; }
            set { SetProperty(ref _CanComSendCount, value); }
        }

        private bool _StartMonitorStopOther;
        public bool StartMonitorStopOther
        {
            get { return _StartMonitorStopOther; }
            set { SetProperty(ref _StartMonitorStopOther, value); }
        }

        private bool _StopMonitorReleaseOther;
        public bool StopMonitorReleaseOther
        {
            get { return _StopMonitorReleaseOther; }
            set { SetProperty(ref _StopMonitorReleaseOther, value); }
        }


        private IEventAggregator _ea;
        public GeneralSettingPar(IEventAggregator ea)
        {
            _ea = ea;
        }
    }


    public class GeneralSettingWindowViewModel : EAViewModelBase
    {
        public static string Getmsg()
        {
            if (IsLoading)
                return "";
            else
                return $"{LangDic["RestartAppOk"]}";
        }
        private static bool IsLoading = true;
        public GeneralSettingWindowViewModel(IContainerExtension co, IRegionManager rm, IEventAggregator ea) : base(co, rm, ea)
        {
            IsLoading = true;
            WinClosingEvent = new DelegateCommand(WinClosingEvent_Sub);
            AccessLvColl = new ObservableCollection<string>();
            AccessLvColl.AddRange(Enum.GetNames(typeof(AccessLv)));
            CanSendModelColl = new ObservableCollection<string>();
            CanSendModelColl.AddRange(Enum.GetNames(typeof(ECANPort.E_CanSendModel)));

            _GSPar = new GeneralSettingPar(ea);
            _GSPar.UserAccessLv = (int)Properties.Settings.Default.CurUserAccessLv;
            _GSPar.REC_MSG_Hz = Properties.Settings.Default.REC_MSG_Hz_ms;
            _GSPar.SEND_MSG_Hz = Properties.Settings.Default.SEND_MSG_Hz_ms;
            _GSPar.ExportReadOnly = Properties.Settings.Default.ExportReadOnly;
            _GSPar.AutoSaveToInherSFM = Properties.Settings.Default.AutoSaveToInherSFM;
            _GSPar.EqualWrite = Properties.Settings.Default.EqualWrite;
            _GSPar.CanSendModel = Properties.Settings.Default.CanSendModel;
            _GSPar.CanComSendCount = Properties.Settings.Default.CanComSendCount;
            _GSPar.StartMonitorStopOther = Properties.Settings.Default.StartMonitorStopOther;
            _GSPar.StopMonitorReleaseOther = Properties.Settings.Default.StopMonitorReleaseOther;
            IsLoading = false;
        }
        public DelegateCommand WinClosingEvent { get; private set; }
        private void WinClosingEvent_Sub()
        {
            Properties.Settings.Default.REC_MSG_Hz_ms = GSPar.REC_MSG_Hz;
            Properties.Settings.Default.SEND_MSG_Hz_ms = GSPar.SEND_MSG_Hz;
            Properties.Settings.Default.CurUserAccessLv = (AccessLv)GSPar.UserAccessLv;
            Properties.Settings.Default.ExportReadOnly = GSPar.ExportReadOnly;
            Properties.Settings.Default.AutoSaveToInherSFM = GSPar.AutoSaveToInherSFM;
            Properties.Settings.Default.EqualWrite = GSPar.EqualWrite;
            Properties.Settings.Default.CanSendModel = GSPar.CanSendModel;
            ECANPort.CanComProc.CanSendModel = (ECANPort.E_CanSendModel)GSPar.CanSendModel;
            Properties.Settings.Default.CanComSendCount = GSPar.CanComSendCount;
            ECANPort.CanComProc.CanComSendCount = GSPar.CanComSendCount;

            Properties.Settings.Default.StartMonitorStopOther = GSPar.StartMonitorStopOther;
            Properties.Settings.Default.StopMonitorReleaseOther = GSPar.StopMonitorReleaseOther;

            Properties.Settings.Default.Save();
        }

        protected override void SFMFileIsReLoaded()
        {

        }


        private GeneralSettingPar _GSPar;
        public GeneralSettingPar GSPar
        {
            get { return _GSPar; }
            set
            {
                SetProperty(ref _GSPar, value);
                _ea.GetEvent<GeneralSettingIsChangeEvent>().Publish(_GSPar);
            }
        }

        public ObservableCollection<string> _AccessLvColl;
        public ObservableCollection<string> AccessLvColl
        {
            get { return _AccessLvColl; }
            set { SetProperty(ref _AccessLvColl, value); }
        }


        public ObservableCollection<string> _CanSendModelColl;
        public ObservableCollection<string> CanSendModelColl
        {
            get { return _CanSendModelColl; }
            set { SetProperty(ref _CanSendModelColl, value); }
        }

    }
}
